package org.balthie.demo.jdk.text.dateformat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SimpleDateFormatTest
{
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    private Date fixDateTime;
    
    private String fixDateTimeStr;
    
    ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 100, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(1000),
            new ThreadFactory() {
                private volatile int count = 1;
                
                @Override
                public Thread newThread(Runnable r)
                {
                    return new Thread(r, "SimpleDateFormatTest-" + count++);
                }
                
            });
    
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormatTest test = new SimpleDateFormatTest();
        test.fixDateTime = new Date(122, 10, 4, 16, 12, 11);
        test.fixDateTimeStr = test.simpleDateFormat.format(test.fixDateTime);
        
        System.out.println(test.fixDateTime);
        
        // test case：simpleDateFormat.parse    
        // result ： [SimpleDateFormatTest-1] 1.[61625607131000] is the same as 2.[45121392731000] [false]
        // parse过程中，会执行       simpleDateFormat.canlendar.clear(); 可能清空 其它线程 set的值
//        test.testParse();
        
        
        // test case：simpleDateFormat.format
        test.testFormat();
        
        TimeUnit.SECONDS.sleep(100);
    }
    
    private void testParse()
    {
        while (true)
        {
            poolExecutor.execute(new Runnable() {
                @Override
                public void run()
                {
                    try
                    {
                        Date parseDate = simpleDateFormat.parse(fixDateTimeStr);
                        System.out.println(String.format(" [%s] 1.[%s] is the same as 2.[%s]  [%s]",
                                Thread.currentThread().getName(), fixDateTime.getTime(), parseDate.getTime(),
                                fixDateTime.getTime() == parseDate.getTime()));
                    }
                    catch (ParseException e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    
    private void testFormat()
    {
        while (true)
        {
            poolExecutor.execute(new Runnable() {
                @Override
                public void run()
                {
                    try
                    {
                        String parseDateStr = simpleDateFormat.format(fixDateTime);
                        System.out.println(String.format(" [%s] 1.[%s] is the same as 2.[%s]  [%s]",
                                Thread.currentThread().getName(), fixDateTimeStr, parseDateStr,
                                fixDateTimeStr.equalsIgnoreCase(parseDateStr)));
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}